/*
 * Copyright 2013 Walter Lütgenau.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package offset.nodes.client.chooser.view;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.JTree;
import javax.swing.UIManager;
import javax.swing.plaf.ColorUIResource;
import javax.swing.text.BadLocationException;
import javax.swing.text.DefaultHighlighter;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeCellRenderer;
import offset.nodes.client.chooser.model.FolderNode;
import offset.nodes.client.chooser.model.TypeIconProvider;

/**
 *
 * @author Walter Lütgenau
 */
public class NodeRenderer extends JPanel implements TreeCellRenderer {

    static ImageIcon defaultIcon = new ImageIcon(offset.nodes.client.chooser.view.NodeRenderer.class.getResource("/offset/nodes/client/view/resources/defaulttype.png"));
    protected JLabel icon;
    protected TreeCellTextArea text;

    public NodeRenderer() {
        setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
        setBackground(Color.WHITE);
        icon = new JLabel() {
            public void setBackground(Color color) {
                if (color instanceof ColorUIResource)
                    color = null;
                super.setBackground(color);
            }
        };
        add(icon);
        add(Box.createHorizontalStrut(4));
        add(text = new TreeCellTextArea());
    }

    public Component getTreeCellRendererComponent(JTree tree, Object value,
            boolean isSelected, boolean expanded, boolean leaf, int row,
            boolean hasFocus) {
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) value;
        ImageIcon nodeIcon = defaultIcon;
        if (treeNode.getUserObject() != null
                && treeNode.getUserObject() instanceof FolderNode) {
            ImageIcon nodeTypeIcon = getNodeTypeIcon((FolderNode) treeNode.getUserObject());
            if (nodeTypeIcon != null)
                nodeIcon = nodeTypeIcon;
        }

        setEnabled(tree.isEnabled());
        text.setText(value.toString());
        text.setSelect(isSelected);
        icon.setIcon(nodeIcon);
        
        return this;
    }

    public Dimension getPreferredSize() {
        Dimension iconD = icon.getPreferredSize();
        Dimension textD = text.getPreferredSize();
        int height = iconD.height < textD.height ? textD.height : iconD.height;
        return new Dimension(iconD.width + textD.width, height);
    }

    public void setBackground(Color color) {
        if (color instanceof ColorUIResource)
            color = null;
        super.setBackground(color);
    }

    /**
     * Return the image icon for the node type
     *
     * @param node the folder node to return the type for
     * @return the image icon
     */
    protected ImageIcon getNodeTypeIcon(FolderNode node) {
        return TypeIconProvider.getInstance().getNodeTypeIcon(node.getType());
    }
}
